package com.cyms.modules.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.base.entity.CompanyEntity;
import com.cyms.modules.base.service.CompanyService;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 车辆公司
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-03 21:30:25
 */
@RestController
@RequestMapping("base/company")
public class CompanyController extends AbstractController {
    @Autowired
    private CompanyService companyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("base:company:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("createOrg",getUser().getCompanyId());
        PageUtils page = companyService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/selectList")
    @RequiresPermissions("base:company:list")
    public R selectList(@RequestParam Map<String, Object> params) {
		// TODO Auto-generated method stub
    	List<CompanyEntity> list=companyService.selectList(new EntityWrapper<CompanyEntity>().eq("create_org",getUser().getCompanyId()));
    	List<Map<String,Object>> data=new ArrayList<>();
    	for (CompanyEntity companyEntity : list) {
    		Map<String, Object> map=new HashMap<>();
			map.put("value", companyEntity.getId());
			map.put("label", companyEntity.getName());
			map.put("type", companyEntity.getType());
			//
			data.add(map);
		}
    	return R.ok().put("companyList", data);
	}
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("base:company:info")
    public R info(@PathVariable("id") Long id){
			CompanyEntity company = companyService.selectById(id);

        return R.ok().put("company", company);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("base:company:save")
    public R save(@RequestBody CompanyEntity company){
        company.setCreateDate(new Date());
        company.setCreateDept(getUser().getDeptId());
        company.setCreateOrg(getUser().getCompanyId());
        company.setCreateUser(getUserId());
        companyService.insert(company);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("base:company:update")
    public R update(@RequestBody CompanyEntity company){
			companyService.updateById(company);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("base:company:delete")
    public R delete(@RequestBody Long[] ids){
			companyService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
